package edu.mju.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.mju.commonutils.JwtUtils;
import edu.mju.commonutils.ReturnCode;
import edu.mju.commonutils.orderVo.CourseWebVoOrder;
import edu.mju.eduservice.client.OrdersClient;
import edu.mju.eduservice.entity.StudyCourse;
import edu.mju.eduservice.service.StudyChapterService;
import edu.mju.eduservice.service.StudyCourseService;
import edu.mju.eduservice.vo.chapter.ChapterVo;
import edu.mju.eduservice.vo.frontvo.CourseFrontVo;
import edu.mju.eduservice.vo.frontvo.CourseWebVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wyp
 * @Date: 2020/9/29 14:10
 */
@RestController
@RequestMapping("/eduservice/front/courseFront")
public class CourseFrontController {

    @Autowired
    private StudyCourseService studyCourseService;

    @Autowired
    private StudyChapterService studyChapterService;

    @Autowired
    private OrdersClient ordersClient;

    /**
     * 分页条件查询课程
     * @param current 当前页
     * @param size 页面大小
     * @param courseFrontVo 条件VO
     * @return json
     */
    @PostMapping("/getFrontCourseList/{current}/{size}")
    public ReturnCode getFrontCourseList(@PathVariable long current, @PathVariable long size,
                                         @RequestBody(required = false)CourseFrontVo courseFrontVo){
        Page<StudyCourse> page = new Page<>(current,size);
        Map<String,Object> map = studyCourseService.getCourseFrontList(page,courseFrontVo);

        System.out.println(map);

        return ReturnCode.success().data(map);
    }


    /**
     * 课程详情
     * @param courseId
     * @param request
     * @return
     */
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public ReturnCode getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = studyCourseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = studyChapterService.getChapterByCourseId(courseId);
        //根据课程id和用户id查询当前课程是否已经支付过了
        boolean buyCourse = ordersClient.isBuyCourse(courseId, JwtUtils.getUserIdByJwtToken(request));

        Map<String,Object> map = new HashMap<>();
        map.put("courseWebVo",courseWebVo);
        map.put("chapterVideoList",chapterVideoList);
        map.put("isBuy",buyCourse);

        return ReturnCode.success().data(map);
    }

    /**
     * 生成订单
     * 根据课程id查询课程信息
     *
     */
    @PostMapping("/getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo = studyCourseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();

        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;

    }
}
